//
//  WebViewController.m
//  PetExchange
//
//  Created by zhibao on 2022/9/26.
//

#import "WebViewController.h"

@interface WebViewController ()<WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler>
@property(nonatomic,strong) WKWebViewConfiguration *config;
@property(nonatomic,strong) UIProgressView *progressView;

@end

@implementation WebViewController

-(instancetype)initWithtitle:(NSString *)subTitle WithUrl:(NSString *)url{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        self.subTitle = subTitle;
        self.url = url;
    }
    return self;
}

-(instancetype)initWithtitle:(NSString *)subTitle WithFileUrl:(NSString *)url{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        self.subTitle = subTitle;
        self.fileUrl = url;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog(@"eeee===%@",self.url);
    _config = [[WKWebViewConfiguration alloc]init];
    WKUserContentController *userContent = [[WKUserContentController alloc]init];
    _config.userContentController = userContent;
    self.gk_navTitle = self.subTitle;
    _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, NAVIH, MY_Screen_W, MY_Screen_H-NAVIH) configuration:_config];
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
    [self.view addSubview:_webView];
    
    self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, NAVIH, MY_Screen_W, 1)];
    self.progressView.tintColor = MYBlock;
    self.progressView.trackTintColor = UIColor.whiteColor;
    [self.view addSubview:self.progressView];
    
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    if (_loadHtml.length > 3) {
        [_webView loadHTMLString:_loadHtml baseURL:nil];
    }else if(_fileUrl.length > 0){
        [_webView loadRequest:[[NSURLRequest alloc]initWithURL:[NSURL fileURLWithPath:self.fileUrl]]];
    }else{
        [_webView loadRequest:[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.url]]];
    }
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    if (_subTitle == nil) {
        self.gk_navTitle = webView.title;
    }else{
        self.gk_navTitle = _subTitle;
    }
    [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.width= '100%'" completionHandler:nil];
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    self.gk_navTitle = _subTitle;
    DLog(@"失败了");
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath  isEqual: @"estimatedProgress"]) {
        _progressView.alpha = 1.0;
        [_progressView setProgress:self.webView.estimatedProgress animated:YES];
        if (self.webView.estimatedProgress >= 1) {
            [UIView animateWithDuration:0.3 delay:0.1 options:2 animations:^{
                self.progressView.alpha = 0;
            } completion:^(BOOL finished) {
                [self->_progressView setProgress:0.0 animated:YES];
            }];
        }
    }
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    
}
- (void)backItemClick:(id)sender {
    if (self.webView.canGoBack) {
        [self.webView goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
   }

@end
